package com.zhu.shiro.sessionManager.memory;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by DELL on 2015/12/30.
 */
public class CustomeSessionDAO extends AbstractSessionDAO{

    private Map<String, Session> sessionMap = new ConcurrentHashMap<String, Session>();

    @Override
    protected Serializable doCreate(Session session) {
        String sessionId = String.valueOf(this.getSessionIdGenerator().generateId(session));

        this.assignSessionId(session, sessionId);

        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable serializable) {
        return sessionMap.get(serializable);
    }

    @Override
    public void update(Session session) throws UnknownSessionException {
        sessionMap.put(String.valueOf(session.getId()), session);
    }

    @Override
    public void delete(Session session) {
        sessionMap.remove(String.valueOf(session.getId()));
    }

    @Override
    public Collection<Session> getActiveSessions() {
        return sessionMap.values();
    }
}
